package cn.com.sparkle.paxos.client.deamon;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import cn.com.sparkle.paxos.client.Command;
import cn.com.sparkle.paxos.client.CommandProcessor;
import cn.com.sparkle.paxos.client.PaxosClient.CommandCallBack;
import cn.com.sparkle.paxos.iomodel.protobuffmessage.PaxosMessages.MasterBeatHeartRequest;
//import cn.com.sparkle.paxos.net.message.MasterBeatHeartRequest;
import cn.com.sparkle.paxos.iomodel.protobuffmessage.PaxosMessages.MessagePackage;
import cn.com.sparkle.paxos.net.NetNode;

public class MasterHeartBeatDeamon extends Thread {
	private final static Logger logger = Logger
			.getLogger(MasterHeartBeatDeamon.class);
	CommandProcessor[] processor;

	public MasterHeartBeatDeamon(CommandProcessor[] processor) {
		this.processor = processor;
	}

	private final CommandCallBack nullCallback = new CommandCallBack() {
		@Override
		public void response(NetNode node, MessagePackage request) {
			// nothing to do
		}
	};

	public void run() {
		while (true) {
			try {
				Command c = new Command(
						MasterBeatHeartRequest.getDefaultInstance(),
						nullCallback);
				for (CommandProcessor p : processor) {
					p.addCommand(c, 1, TimeUnit.MICROSECONDS);
				}
				logger.debug("send a heartbeat to master");
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
